const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const common = require('../../../utils/common.js');

//获取应用实例
const app = getApp();
var recordInterval = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",//type对应的id
    type: "",//类型：参考util.getShareType方法
    shareOpenid: "",// 分享人的openid
    loginDialogShow: false,// 控制登录的弹窗是否显示
    isCheckLogin: 0,// 是否需要校验登录：0否  1是
    circleid: '',//圈子id
    data: "",//数据（用于扩展）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var system = {};
    system.isShare = true;
    app.globalData.system = system;

    var _this = this;
    var id = options.id;//type对应的id
    var type = options.type;//类型
    var shareOpenid = options.shareOpenid;// 分享人的openid
    var isCheckLogin = options.isCheckLogin;// 是否需要校验登录：0否  1是
    var circleid = options.circleid;
    var data = options.data;
    var openid = app.globalData.logininfo.openid;
    _this.setData({
      id: id,
      type: type == null ? "" : parseInt(type),
      shareOpenid: shareOpenid == null ? "" : shareOpenid,
      isCheckLogin: isCheckLogin == null ? 0 : isCheckLogin,
      circleid: circleid == null ? "" : circleid,
      data: data == null ? "" : data
    })

    if (openid != null && openid != "") {
      // 已登录
      _this.businessCode();
    }else{
      // 未登录，需要先进行登录
      if (isCheckLogin == "1"){
        _this.setData({
          loginDialogShow: true
        })
      } else {
        _this.businessCode();
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 重定向到首页
   */
  switchToIndex: function(){
    wx.switchTab({
      url: '../../home/home',
    })
  },

  /**
   * 登录
   */
  doLogin: function (e) {
    var _this = this;
    common.doLogin(e, _this);
    _this.doRecordInterval();
  },

  /**
   * 定时器
   */
  doRecordInterval: function(){
    var _this = this;
    if (recordInterval == null) {
      recordInterval = setInterval(function () {
        var openid = app.globalData.logininfo.openid;
        if (openid != null && openid != "") {
          _this.businessCode();
        }
      }, 1000);
    }
  },

  /**
   * 业务代码
   */
  businessCode: function(){
    var _this = this;
    var type = _this.data.type;//类型：1圈子 2日记 3帮助补签到
    switch (type) {
      case util.getShareType("circle"):
        // 圈子
        _this.navigateToCircleInfo();
        break;
      case util.getShareType("diary"):
        // 日记
        _this.navigateToDiaryDetails();
        break;
      case util.getShareType("helpSign"):
        // 3帮助补签到
        _this.navigateToHelpSign();
        break;
      case util.getShareType("groupbuyTicket"):
        // 4团购票种分享进入
        _this.navigateToCircledetails();
        break;
      case util.getShareType("examplesTicket"):
        // 5赠送票种
        _this.navigateToGivingticket();
        break;
      case util.getShareType("commissionCircle"):
        // 5分销圈子
        _this.navigateToCircledetails_comm();
        break;
    }
  },

  /**
   * 进入日记详情页面
   */
  navigateToDiaryDetails: function () {
    var _this = this;
    wx.redirectTo({
      url: "../../../../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/diaryDetails/diaryDetails?circleid=" + _this.data.circleid + "&diaryid=" + _this.data.id,
    })
  },
  /**
   * 进入帮助补签到页面
   */
  navigateToHelpSign: function () {
    var _this = this;
    wx.redirectTo({
      url: "../../../../../../../grade/pages/grade/sign/helpSign/helpSign?shareOpenid=" + _this.data.shareOpenid + "&data=" + _this.data.data,
    })
  },

  /**
  * 进入圈子详情页面
  */
  navigateToCircleInfo: function () {
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    var data = {};
    data.circleid = _this.data.id;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinApiUrl, data);
      })
    } else {
      _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinUrl, data);
    }
  },

  /**
   * 校验用户是否为已加入的圈子：请求
   */
  userIsCircleJoinQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      var userIsCircleJoin = false;
      if (res.code == 200) {
        userIsCircleJoin = res.result.userIsCircleJoin;
      }
      if (userIsCircleJoin) {
        // 已加入
        wx.redirectTo({
          url: '../../../../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/circleinteraction?isShare=1&circleid=' + data.circleid,
        })
      } else {
        // 未加入
        wx.redirectTo({
          url: '../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?circleid=' + data.circleid + "&invitationOpenid=" + _this.data.shareOpenid,
        })
      }
    });
  },

  /**
   * 邀请好友购买团购订单：进入圈子详情页
   */
  navigateToCircledetails: function () {
    var _this = this;
    var data = JSON.parse(_this.data.data);
    wx.redirectTo({
      url: '../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?circleid=' + _this.data.circleid + "&invitationOpenid=" + _this.data.shareOpenid + "&shareAmountDetailsGroupbuyId=" + _this.data.id + "&shareAmountDetailsId=" + data.amountdetailsid,
    })
  },

  /**
   * 分销圈子：进入圈子详情页
   */
  navigateToCircledetails_comm: function () {
    var _this = this;
    wx.redirectTo({
      url: '../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?circleid=' + _this.data.circleid + "&invitationOpenid=" + _this.data.shareOpenid + "&distributionOpenid=" + _this.data.shareOpenid,
    })
  },
  

  /**
   * 赠送票种：进入票种接受页面
   */
  navigateToGivingticket: function () {
    var _this = this;
    wx.redirectTo({
      url: '../../../interest/pages/interest/givingticket/givingticket?circleid=' + _this.data.circleid + "&shareOpenid=" + _this.data.shareOpenid + "&joinDetailId=" + _this.data.id,
    })
  },
})